#!/bin/bash

#>>-----------------------------------------------------------------------------------
#顶层项目目录
export PROJECT_TOPDIR=$PWD
#顶层当前配置文件
export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
#-------------------------------------------------------------------------------------

#>>topdir-----------------------------------------------------------------------------
#OEMKIT顶层目录
export OEMKIT_TOPDIR=${PROJECT_TOPDIR}/a.OEMKIT
#型号配置目录
export OEMKIT_DEVICES_DIR=${OEMKIT_TOPDIR}/devices
#脚本文件目录
export OEMKIT_SCRIPTS_DIR=${OEMKIT_TOPDIR}/scripts

#GCCKIT顶层目录
export GCCKIT_TOPDIR=${PROJECT_TOPDIR}/b.GCCKIT

#SYSKIT顶层目录
export SYSKIT_TOPDIR=${PROJECT_TOPDIR}/c.SYSKIT

#MIDKIT顶层目录
export MIDKIT_TOPDIR=${PROJECT_TOPDIR}/d.MIDKIT

#APPKIT顶层目录
export APPKIT_TOPDIR=${PROJECT_TOPDIR}/e.APPKIT

#MGRKIT顶层目录
export MGRKIT_TOPDIR=${PROJECT_TOPDIR}/m.MGRKIT

#KNFKIT顶层目录
export KNFKIT_TOPDIR=${PROJECT_TOPDIR}/k.KNFKIT

#RCSKIT顶层目录
export RCSKIT_TOPDIR=${PROJECT_TOPDIR}/f.RCSKIT

#DEVKIT顶层
export DEVKIT_TOPDIR=${PROJECT_TOPDIR}/w.DEVKIT

#IMGBINS顶层
export IMGBINS_TOPDIR=${PROJECT_TOPDIR}/y.IMGBINS

#OTAKIT顶层目录
export OTAKIT_TOPDIR=${PROJECT_TOPDIR}/o.OTAKIT

#-------------------------------------------------------------------------------------

#>>multiprocess-----------------------------------------------------------------------
#计算多线程编译等级MKCL
cpu_cores=`cat /proc/cpuinfo | grep "processor" | wc -l`
if [ ${cpu_cores} -le 8 ] ; then
	export MKCL=${cpu_cores}
else
	export MKCL=`expr ${cpu_cores} / 2`
fi
#-------------------------------------------------------------------------------------

#定义ARMGCC默认目录
export GCCKIT_ARMGCC_DEFPATH=${GCCKIT_TOPDIR}/armgcc
if [ ! -e $GCCKIT_ARMGCC_DEFPATH ]; then
	swarn "envsetup_common: 检测: GCCKIT_ARMGCC_DEFPATH: $GCCKIT_ARMGCC_DEFPATH [失败]"
fi

#定义SYSROOT默认目录
export GCCKIT_SYSROOT_DEFPATH=${GCCKIT_TOPDIR}/sysroot
if [ ! -e $GCCKIT_SYSROOT_DEFPATH ]; then
	swarn "envsetup_common: 检测: GCCKIT_SYSROOT_DEFPATH: $GCCKIT_SYSROOT_DEFPATH [失败]"
fi

#自动检查编译工具链前缀
export GCCKIT_PREFIX_DEFNAME=arm-linux
if [ -f $GCCKIT_TOPDIR/envsetup_gcckit.sh ]; then
	source $GCCKIT_TOPDIR/envsetup_gcckit.sh
	if [ -n "$GCCKIT_PREFIX" ]; then
		export GCCKIT_PREFIX_DEFNAME=${GCCKIT_PREFIX}
	else
		serro "envsetup_common: 检测: GCCKIT_PREFIX_DEFNAME\t: $GCCKIT_PREFIX_DEFNAME [失败]"
		exit 1
	fi
else
	swarn "envsetup_common: 默认GCCKIT编译器前缀: ${GCCKIT_PREFIX_DEFNAME}"
fi

#检查编译环境
check_env()
{
	sinfo "-------------------------------------------------------------------------------------"
	sinfo "envsetup_common:check_env: 检查编译环境... [开始]"
	sinfo "-------------------------------------------------------------------------------------"
	export GCC_PATH=$GCCKIT_ARMGCC_DEFPATH
	export GCC_TYPE=$GCCKIT_PREFIX_DEFNAME
	export GCC_SYSROOT=$GCCKIT_SYSROOT_DEFPATH
	export HOST=$GCC_TYPE
	export TARGET=$GCC_TYPE
	export CC="${HOST}-gcc"
	export CXX="${HOST}-g++"
	export OBJC="${CC}"
	export LD="${HOST}-ld"
	export ARCH=arm
	export CROSS_COMPILE=${HOST}-

	sinfo "envsetup_common:check_env: 检查编译器: $GCC_PATH [开始]"
	if [ -d $GCC_PATH ]; then
		export PATH=$GCC_PATH/bin:$PATH
		$GCC_TYPE-gcc -v > /dev/null  2>&1
		if [ $? -eq 0 ]; then
			sdone "envsetup_common:check_env: 测试编译器: ${GCC_TYPE}-gcc [完成]"
		else
			serro "envsetup_common:check_env: 测试编译器: ${GCC_TYPE}-gcc [错误]"
			exit 1
		fi
		sdone "envsetup_common:check_env: 检查编译器: $GCC_PATH [完成]"
	else
		serro "envsetup_common:check_env: 检查编译器: $GCC_PATH [失败]"
		exit 1
	fi

	sinfo "-------------------------------------------------------------------------------------"
	swarn "envsetup_common:check_env: 目标机架构: ARCH          : $ARCH"
	swarn "envsetup_common:check_env: 目标机主机: HOST          : $HOST"
	swarn "envsetup_common:check_env: 编译器类型: GCC_TYPE      : $GCC_TYPE"
	swarn "envsetup_common:check_env: 编译器路径: GCC_PATH      : $GCC_PATH"
	swarn "envsetup_common:check_env: 编译器前缀: CROSS_COMPILE : $CROSS_COMPILE"
	sinfo "-------------------------------------------------------------------------------------"
	sdone "envsetup_common:check_env: 检查编译环境... [完毕]"
	sinfo "-------------------------------------------------------------------------------------"
}

#修改赋值行,例如:ABC=1修改为ABC=2
change_value_line()
{
	#$1: 参数1 被修改文件名称
	#$2: 参数2 关键字名称 例如某行ABC=1其中ABC即为关键字
	#$3: 参数3 替换字符
	#$4: 参数4 是否附加双引号 
    #    例如: 
	#    原本ABC=1替换为ABC=2，
	#    当参数4为1时,ABC=1替换为ABC="2"
	#$5: 参数5: 是否使用追加符号 
    #    例如: 
	#    原本ABC=1替换为ABC=2，
	#    当参数5为1时,ABC=1替换为ABC+=2

	if [ $# -ne 5 ]; then
		serro "change_value_line: 参数个数错误: $#"
		exit 1
	fi
	
	if [ ! -f $1 ]; then
		serro "change_value_line: 目标文件丢失: $1"
		exit 1
	fi

	local line_number=$(grep -wn "^$2" $1 | cut -d: -f 1)
	if [ -z  "$line_number" ]; then
		serro "change_value_line: 没有找到关键字: $2"
		exit 1
	fi

	if [ $4 -eq 1 ]; then
		if [ $5 -eq 1 ]; then
			sed -i "${line_number}c $2+=\"$3\"" $1
		else
			sed -i "${line_number}c $2=\"$3\"" $1
		fi
	    [ $? -ne 0 ] && serro "change_value_line: 修改失败: $1: $2 ->  $3" && exit 1
	else
		if [ $5 -eq 1 ]; then
			sed -i "${line_number}c $2+=$3" $1
		else
			sed -i "${line_number}c $2=$3"  $1
		fi
	    [ $? -ne 0 ] && serro "change_value_line: 修改失败: $1: $2 ->  $3" && exit 1
	fi
	return 0
}

#修改定义行,例如:#define ABC 1修改为#define ABC 2
change_define_line()
{
	#$1: file path
	#$2:  KeyString, example: line is: #define ABC  1 KeyString is: ABC
	#$3:  ReplaceString, 
	#$4:  if $4 = 1 then change #define ABC 1  change to #define ABC "1"

	#$1: 参数1 被修改文件名称
	#$2: 参数2 关键字名称 例如某行#define ABC  1其中ABC即为关键字
	#$3: 参数3 替换字符
	#$4: 参数4 是否附加双引号 
    #    例如: 
	#    原本#define ABC 1替换为#defie ABC 2
	#    当参数4为1时,#define ABC 1替换为#define ABC "2"
	if [ $# -ne 4 ]; then
		serro "change_define_line: 参数个数错误: $#"
		exit 1
	fi
	
	if [ ! -f $1 ]; then
		serro "change_define_line: 目标文件丢失: $1"
		exit 1
	fi

	local line_number=$(grep -wn "^#define $2" $1 | cut -d: -f 1)
	if [ -z  "$line_number" ]; then
		serro "change_define_line: 没有找到关键字: $2"
		exit 1
	fi

	if [ $4 -eq 1 ]; then
		sed -i "${line_number}c #define $2 \"$3\"" $1
	    [ $? -ne 0 ] && serro "change_define_line: 修改失败: $1: $2 ->  $3" && exit 1
	else
		sed -i "${line_number}c #define $2 $3"  $1
	    [ $? -ne 0 ] && serro "change_define_line: 修改失败: $1: $2 ->  $3" && exit 1
	fi
	return 0
}
#-----------------------------------------------------------------------
